import sys, os
import shutil

# only train set , test set for eval
in_dir = '/home/leo/Downloads/datas/road_car/all_set4k/'
out_dir = '../train_data/'

if __name__ == '__main__':
    out_img_path = os.path.join(out_dir, 'img')
    out_xml_path = os.path.join(out_dir, 'xml')

    if os.path.exists(out_img_path) is False:
        os.makedirs(out_img_path)
    if os.path.exists(out_xml_path) is False:
        os.makedirs(out_xml_path)

    dir_num = len(os.listdir(in_dir))
    cnt = 0
    for dir in os.listdir(in_dir):
        cnt+=1
        print(" processing dir {} of {}".format(cnt,dir_num))
        for file in os.listdir(os.path.join(in_dir, dir)):
            if os.path.splitext(file)[1] in ['.jpeg', '.jpg', '.png', '.JPEG', '.PNG', '.BMP', '.bmp']:
                try:
                    shutil.copy(os.path.join(in_dir, dir, file), os.path.join(out_img_path, dir + "_" + file))
                    shutil.copy(os.path.join(in_dir, dir, os.path.splitext(file)[0]+".xml"),
                                os.path.join(out_xml_path, dir + "_" + os.path.splitext(file)[0]+".xml"))
                except Exception as ex :
                    print(str(ex))
